#pragma  once


#include "TListDlg.h"
#include "LogMng.h"
#include "RecvDlg.h"

class TMainWin : public TWin {
protected:
	static HICON	hMainIcon;
	static HICON	hRevIcon;
	static TMainWin	*mainWin;	// for thread proc

	TList			sendList;
	TList			recvList;
	TList			msgList;
	TList			sendFileList;
	TList			connectList;
	THosts			hosts;

	TSetupDlg		*setupDlg;
	TAboutDlg		*aboutDlg;
	TShareStatDlg	*shareStatDlg;
	TAbsenceDlg		*absenceDlg;
	MsgMng			*msgMng;
	LogMng			*logmng;
	ShareMng		*shareMng;
	Cfg				*cfg;
	TRecycleList	*ansList;

	int			portNo;
	int			memberCnt;
	time_t		refreshStartTime;
	time_t		entryStartTime;
	UINT		entryTimerStatus;
	UINT		reverseTimerStatus;
	UINT		ansTimerID;
	UINT		TaskBarCreateMsg;
	BOOL		terminateFlg;
	BOOL		activeToggle;

#define MAX_PACKETLOG	16
	struct {
		ULONG		no;
		ULONG		addr;
		int			port;
	} packetLog[MAX_PACKETLOG];
	int		packetLogCnt;

	BOOL	IsLastPacket(MsgBuf *msg);
	void	SetIcon(HICON hSetIcon);
	void	ReverseIcon(BOOL startFlg);

	void	EntryHost(void);
	void	ExitHost(void);
	void	Popup(UINT resId);
	BOOL	PopupCheck(void);
	BOOL	AddAbsenceMenu(HMENU hMenu, int insertIndex);
	void	ActiveChildWindow(BOOL hide=FALSE);
	BOOL	TaskBar(int nimMode, HICON hSetIcon = NULL, LPCSTR tip = NULL);
	BOOL	UdpEvent(LPARAM lParam);
	BOOL	TcpEvent(SOCKET sd, LPARAM lParam);

	BOOL	CheckConnectInfo(ConnectInfo *conInfo);
	inline	SendFileObj *FindSendFileObj(SOCKET sd);
	BOOL	StartSendFile(SOCKET sd);
	BOOL	OpenSendFile(const char *fname, SendFileObj *obj);
	static DWORD WINAPI SendFileThread(void *_sendFileObj);
	BOOL	SendFile(SendFileObj *obj);
	BOOL	SendDirFile(SendFileObj *obj);
	BOOL	CloseSendFile(SendFileObj *obj);
	BOOL	EndSendFile(SendFileObj *obj);

	void	BroadcastEntry(ULONG mode);
	void	BroadcastEntrySub(ULONG addr, int portNo, ULONG mode);
	void	BroadcastEntrySub(HostSub *hostSub, ULONG mode);
	void	Terminate(void);

	BOOL	SendDlgOpen(HWND hRecvWnd = NULL, MsgBuf *msg = NULL);
	void	SendDlgHide(TSendDlg *sendDlg);
	void	SendDlgExit(TSendDlg *sendDlg);
	BOOL	RecvDlgOpen(MsgBuf *msg);
	void	RecvDlgExit(TRecvDlg *recvDlg);
	void	MsgDlgExit(TMsgDlg *msgDlg);
	void	MiscDlgOpen(TDlg *dlg);
	void	LogOpen(void);

	void	AddHost(HostSub *hostSub, ULONG command, char *nickName="", char *groupName="");
	inline	void SetHostData(Host *destHost, HostSub *hostSub, ULONG command, time_t now_time, char *nickName="", char *groupName="", int priority=DEFAULT_PRIORITY);
	void	DelAllHost(void);
	void	DelHost(HostSub *hostSub);
	void	DelHostSub(Host *host);
	void	RefreshHost(BOOL unRemove);
	void	SetCaption(void);
	void	SendHostList(MsgBuf *msg);
	void	AddHostList(MsgBuf *msg);
	ULONG	HostStatus(void);
	void	ActiveListDlg(TList *dlgList, BOOL active = TRUE);
	void	DeleteListDlg(TList *dlgList);
	void	ActiveDlg(TDlg *dlg, BOOL active = TRUE);
	char	*GetNickNameEx(void);
	void	InitIcon(void);
	void	ControlIME(TWin *win, BOOL on);
	BOOL	SetAnswerQueue(AddrObj *obj);
	void	ExecuteAnsQueue(void);
	BOOL	SetupCryptAPI(void);
	// ctl_flg
#define		KEY_REBUILD		0x0001
#define		KEY_DIAG		0x0002
	BOOL	SetupCryptAPICore(int ctl_flg = 0);
	BOOL	SetupRSAKey(int bitLen, int ctl_flg = 0);
	BOOL	LoadPrivBlob(BYTE *rawBlob, int *rawBlobLen);
	BOOL	StorePrivBlob(BYTE *rawBlob, int rawBlobLen);

	void	MsgBrEntry(MsgBuf *msg);
	void	MsgBrExit(MsgBuf *msg);
	void	MsgAnsEntry(MsgBuf *msg);
	void	MsgBrAbsence(MsgBuf *msg);
	void	MsgSendMsg(MsgBuf *msg);
	void	MsgRecvMsg(MsgBuf *msg);
	void	MsgReadMsg(MsgBuf *msg);
	void	MsgBrIsGetList(MsgBuf *msg);
	void	MsgOkGetList(MsgBuf *msg);
	void	MsgGetList(MsgBuf *msg);
	void	MsgAnsList(MsgBuf *msg);
	void	MsgGetInfo(MsgBuf *msg);
	void	MsgSendInfo(MsgBuf *msg);
	void	MsgGetPubKey(MsgBuf *msg);
	void	MsgAnsPubKey(MsgBuf *msg);
	void	MsgGetAbsenceInfo(MsgBuf *msg);
	void	MsgSendAbsenceInfo(MsgBuf *msg);
	void	MsgReleaseFiles(MsgBuf *msg);
	void	MsgInfoSub(MsgBuf *msg);

public:
	TMainWin(ULONG _nicAddr=INADDR_ANY, int _portNo=IPMSG_DEFAULT_PORT, TWin *_parent = NULL);
	virtual ~TMainWin();

	virtual BOOL	EvCreate(LPARAM lParam);
	virtual BOOL	EvClose(void);
	virtual BOOL	EvCommand(WORD wNotifyCode, WORD wID, LPARAM hwndCtl);
	virtual BOOL	EvSysCommand(WPARAM uCmdType, POINTS pos);
	virtual BOOL	EvTimer(WPARAM timerID, TIMERPROC proc);
	virtual BOOL	EvEndSession(BOOL nSession, BOOL nLogOut);
	virtual BOOL	EvQueryOpen(void);
	virtual BOOL	EvHotKey(int hotKey);
	virtual BOOL	EventButton(UINT uMsg, int nHitTest, POINTS pos);
	virtual BOOL	EventInitMenu(UINT uMsg, HMENU hMenu, UINT uPos, BOOL fSystemMenu);
	virtual BOOL	EventUser(UINT uMsg, WPARAM wParam, LPARAM lParam);

	static	HICON	GetIPMsgIcon(void);

	//added by wallimn
	//virtual LRESULT WinProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
	//HRESULT OnRTCEvent(UINT message, WPARAM wParam, LPARAM lParam);
	//HRESULT InitRTCClient();
	//HRESULT OnRTCMessagingEvent (IRTCMessagingEvent *pEvent);
	//HRESULT AddSession(IRTCSession *pSession, RTC_SESSION_TYPE enType);
	//IRTCParticipant * m_Participant;
	//HRESULT MakeCall(RTC_SESSION_TYPE enType, BSTR bstrURI);
	//void OnRTCClientEvent(IRTCClientEvent *pEvent);
	//void OnRTCIntensityEvent(IRTCIntensityEvent *pEvent);
	//void OnRTCMediaEvent(IRTCMediaEvent *pEvent);
	//void OnRTCSessionStateChangeEvent(IRTCSessionStateChangeEvent *pEvent);
	//CRTCEvents * m_pEvents;
	//IRTCClient * m_pClient;
	//TAVDlg *m_AVDlg;

};

HWND GetMainWnd(void);

char *GetVerionStr(void);